package zdy.web.str;

import static org.junit.Assert.*;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class xmldemoTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void text() {

		String path = System.getProperty("user.dir") + "/src/test/resources/test.xml";
		createXML(path);
		select(path);
		add(path);
		del(path,0);
		edit(path,0);
		selectAttr(path);
	}

	public void createXML(String path) {
		Document doc = DocumentHelper.createDocument();
		Element root = doc.addElement("root");
		Element father = root.addElement("father");
		Element child = father.addElement("child");
		Element name = child.addElement("name");
		name.setText("ʮ���ʻ�");
		Element sex = child.addElement("sex");
		sex.setText("��");

		try {

			PrintWriter pw = new PrintWriter(path);
			XMLWriter xw = new XMLWriter(pw);
			xw.write(doc);

			xw.flush();
			xw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			System.out.println("Over");
		}
	}

	public void createXML2(String path) {
		try {
			Document doc = DocumentHelper.createDocument();
			// ���Ӹ��ڵ�
			Element books = doc.addElement("books");
			// ������Ԫ��
			Element book1 = books.addElement("book");
			Element title1 = book1.addElement("title");
			Element author1 = book1.addElement("author");

			Element book2 = books.addElement("book");
			Element title2 = book2.addElement("title");
			Element author2 = book2.addElement("author");

			// Ϊ�ӽڵ��������
			book1.addAttribute("id", "001");
			// ΪԪ���������
			title1.setText("java ����");
			author1.setText("����ʦ");

			book2.addAttribute("id", "002");
			title2.setText("java ���Ա���");
			author2.setText("Erik T. Ray");

			// ʵ���������ʽ����
			OutputFormat format = OutputFormat.createPrettyPrint();
			// �����������
			format.setEncoding("UTF-8");
			// ������Ҫд���File����
			File file = new File("D:" + File.separator + "books.xml");
			// ����XMLWriter���󣬹��캯���еĲ���Ϊ��Ҫ������ļ����͸�ʽ

			XMLWriter xw = new XMLWriter(new FileOutputStream(path), format);
			// ��ʼд�룬write�����а������洴����Document����
			xw.write(doc);
			xw.flush();
			xw.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			System.out.println("Over");
		}
	}

	public void selectAttr(String path) {

		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(new File(path));
			Element root = document.getRootElement();

			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element element = (Element) it.next();

				// δ֪�������������
				/*
				 * Iterator attrIt = element.attributeIterator(); while
				 * (attrIt.hasNext()) { Attribute a = (Attribute) attrIt.next();
				 * System.out.println(a.getValue()); }
				 */

				// ��֪�������������
				System.out.println("id: " + element.attributeValue("id"));

				// δ֪Ԫ���������
				/*
				 * Iterator eleIt = element.elementIterator(); while
				 * (eleIt.hasNext()) { Element e = (Element) eleIt.next();
				 * System.out.println(e.getName() + ": " + e.getText()); }
				 * System.out.println();
				 */

				// ��֪Ԫ���������
				System.out.println("title: " + element.elementText("title"));
				System.out.println("author: " + element.elementText("author"));
				System.out.println();

			}
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void add(String path) {

		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element root = document.getRootElement();
			Element parent = root.addElement("linkman");
			// parent.setAttribute(new QName("id"), "2");
			// parent.addAttribute(new QName("id"), "2");
			parent.addAttribute("id", "2");
			Element name = parent.addElement("name");
			name.setText("����");
			Element email = parent.addElement("email");
			email.setText("lichao511216@163.com");
			Element gender = parent.addElement("gender");
			gender.setText("female");
			OutputFormat out = new OutputFormat("  ", true, "utf-8");
			XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"), out);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void del(String path, int i) {

		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element root = document.getRootElement();

			@SuppressWarnings("unchecked")
			Iterator<Element> it = root.elementIterator();

			while (it.hasNext()) {
				Element e = it.next();
				if (Integer.parseInt(e.attributeValue("id")) == i) {
					root.remove(e);
					break;
				}

			}

			OutputFormat out = new OutputFormat("  ", true, "utf-8");
			XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"), out);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	private void edit(String path, int i) {

		try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element root = document.getRootElement();

			@SuppressWarnings("unchecked")
			Iterator<Element> it = root.elementIterator();

			while (it.hasNext()) {
				Element e = it.next();
				if (Integer.parseInt(e.attributeValue("id")) == i) {
					e.element("name").setText("���");
					e.element("email").setText("lichao51126@gmail.com");
					break;
				}

			}

			OutputFormat out = new OutputFormat("  ", true, "utf-8");
			XMLWriter writer = new XMLWriter(new FileOutputStream("address.xml"), out);
			writer.write(document);
			writer.close();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}
	private  void select(String path)  {  
        try {
			SAXReader reader = new SAXReader();
			Document document = reader.read(new File(path));
			Element root = document.getRootElement();
			//@SuppressWarnings("unchecked")
			Iterator<Element> it = root.elementIterator();
			while (it.hasNext()) {
				Element e = it.next();
				System.out.println("id ��" + e.attributeValue("id"));
				System.out.println("������ " + e.element("name").getText());
				System.out.println("���䣺" + e.elementText("email"));
				System.out.println("�Ա�" + e.elementText("gender"));
			} 
		} catch (Exception e) {
			// TODO: handle exception
		}  
 
    }  
}
